package com.settlement.system.service.impl;

import com.settlement.system.model.entity.OdsErpRetailSalesOrderDetails;
import com.settlement.system.mapper.OdsErpRetailSalesOrderDetailsMapper;
import com.settlement.system.service.OdsErpRetailSalesOrderDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.OdsErpRetailSalesOrderDetailsQuery;
import com.settlement.system.model.form.OdsErpRetailSalesOrderDetailsForm;
import com.settlement.system.model.vo.OdsErpRetailSalesOrderDetailsImportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;

/**
 * <p>
 * ERP零售销货单明细表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-08-02
 */
@Service
public class OdsErpRetailSalesOrderDetailsServiceImpl extends ServiceImpl<OdsErpRetailSalesOrderDetailsMapper, OdsErpRetailSalesOrderDetails> implements OdsErpRetailSalesOrderDetailsService {

    @Override
    public Page<OdsErpRetailSalesOrderDetails> getPage(OdsErpRetailSalesOrderDetailsQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<OdsErpRetailSalesOrderDetails> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveOdsErpRetailSalesOrderDetails(OdsErpRetailSalesOrderDetailsForm odsErpRetailSalesOrderDetailsForm) {
         OdsErpRetailSalesOrderDetails odsErpRetailSalesOrderDetails = new OdsErpRetailSalesOrderDetails();
         BeanUtils.copyProperties(odsErpRetailSalesOrderDetailsForm, odsErpRetailSalesOrderDetails);
         return this.saveOrUpdate(odsErpRetailSalesOrderDetails);
     }

     @Override
     public OdsErpRetailSalesOrderDetailsForm getOdsErpRetailSalesOrderDetailsFormData(Long id) {
         OdsErpRetailSalesOrderDetails odsErpRetailSalesOrderDetails = this.getById(id);
         OdsErpRetailSalesOrderDetailsForm odsErpRetailSalesOrderDetailsForm = new OdsErpRetailSalesOrderDetailsForm();
         BeanUtils.copyProperties(odsErpRetailSalesOrderDetails, odsErpRetailSalesOrderDetailsForm);
         return odsErpRetailSalesOrderDetailsForm;
     }

     @Override
     public boolean deleteOdsErpRetailSalesOrderDetails(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<OdsErpRetailSalesOrderDetailsImportVo> voList)  {
        List<OdsErpRetailSalesOrderDetails> odsErpRetailSalesOrderDetailsList = new ArrayList<>();
        for (OdsErpRetailSalesOrderDetailsImportVo importVo : voList) {
            OdsErpRetailSalesOrderDetails odsErpRetailSalesOrderDetails = new OdsErpRetailSalesOrderDetails();
            BeanUtils.copyProperties(importVo, odsErpRetailSalesOrderDetails);
            odsErpRetailSalesOrderDetailsList.add(odsErpRetailSalesOrderDetails);
        }
        this.saveOrUpdateBatch(odsErpRetailSalesOrderDetailsList);
    }

    @Override
    public List<OdsErpRetailSalesOrderDetailsImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<OdsErpRetailSalesOrderDetails> odsErpRetailSalesOrderDetailsList = this.getBaseMapper().selectBatchIds(ids);
        List<OdsErpRetailSalesOrderDetailsImportVo> voList = new ArrayList<>();
        for (OdsErpRetailSalesOrderDetails odsErpRetailSalesOrderDetails : odsErpRetailSalesOrderDetailsList) {
            OdsErpRetailSalesOrderDetailsImportVo vo = new OdsErpRetailSalesOrderDetailsImportVo();
            BeanUtils.copyProperties(odsErpRetailSalesOrderDetails, vo);
            voList.add(vo);
        }
        return voList;
    }
}
